package com.controller.sys;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.EasyuiUtils;
import com.common.StringUtils;
import com.dao.sys.SysModuleMapper;
import com.dao.sys.SysRoleMapper;
import com.dao.sys.SysRoleModulePermissionMapper;
import com.model.sys.SysModule;
import com.model.sys.SysRole;
import com.model.sys.SysRoleModulePermission;
import com.service.sys.SysLogService;
import com.service.sys.SysService;

@Controller
public class RoleManageController {
	static final Logger logger = LoggerFactory.getLogger(RoleManageController.class);
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	@Autowired
	private SysLogService sysLogService;
	@Autowired
	private SysService sysService;
	@Autowired
	private SysRoleModulePermissionMapper sysRoleModulePermissionMapper;
	@Autowired
	private SysModuleMapper sysModuleMapper;

	@RequestMapping("/sys/RoleManage")
	public Map<String, String> getMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		map.put("key2", "value-2");
		map.put("key3", "value-2");
		return map;
	}

	@RequestMapping(value = "/RoleManage/RoleListJson_combogrid", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String RoleListJson_combogrid(HttpServletRequest request) {
		try {
			List<SysRole> list = sysRoleMapper.selectAll();
			// JSONArray jsonarray = JSONArray.fromObject(list);
			String jsonString = JSON.toJSONString(list);
			// System.out.println("RoleListJson:" + jsonString);
			logger.info("RoleListJson:{}", jsonString);
			// request.setAttribute("addLists", list);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/RoleManage/RoleListJson", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String RoleListJson(HttpServletRequest request) {
		try {
			List<SysRole> list = sysRoleMapper.selectAll();
			// JSONArray jsonarray = JSONArray.fromObject(list);
			String jsonString = JSON.toJSONString(list);
			// System.out.println("RoleListJson:" + jsonString);
			logger.info("RoleListJson:{}", jsonString);
			// request.setAttribute("addLists", list);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/RoleManage/GetModule_Role", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String GetModule_Role(HttpServletRequest request) {
		try {
			String roleid = request.getParameter("roleid");
			// List<SysRole> listRole = sysRoleMapper.selectAll();
			List<SysModule> listModules = sysModuleMapper.selectAll();
			List<SysRoleModulePermission> listPermissions = sysRoleModulePermissionMapper.selectAllByRoleid(roleid);
			String allow = "";
			for (SysModule module : listModules) {
				for (SysRoleModulePermission permission : listPermissions) {
					if (module.getModuleid().equals(permission.getModuleid())) {
						allow += permission.getPermission() + ",";
					}
				}
				module.setActionallowstring(StringUtils.sideTrim(allow, ","));
				allow = "";
			}
			// JSONArray jsonarray = JSONArray.fromObject(listModules);
			String jsonString = EasyuiUtils.getTreeGridJson(listModules, "moduleid", "parentmoduleid", "0");
			// System.out.println("RoleListJson:" + jsonString);
			logger.info("RoleListJson:{}", jsonString);
			// request.setAttribute("addLists", list);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/RoleManage/AddRole", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String AddRole(HttpServletRequest request) throws IOException, Exception {
		try {
			String resultString = "保存成功!";
			SysRole model = new SysRole();
			String czr = request.getParameter("czr");
			String id = request.getParameter("id");
			model.setRolename(request.getParameter("name"));
			model.setRolecomment(request.getParameter("bz"));
			model.setRolesort(StringUtils.parseBD(request.getParameter("xh")));
			String qx = request.getParameter("qx");
			model.setDisabled(request.getParameter("jy"));
			model.setIstogether(request.getParameter("together"));

			if (id == null || id.equals("")) {
				id = UUID.randomUUID().toString();
				model.setRoleid(id);
				resultString = sysService.addRole(model, qx) > 0 ? "保存成功!" : "保存失败!";
				sysLogService.add(czr, "", "新增角色_" + id, "", "", "", "");
			} else {
				model.setRoleid(id);
				resultString = sysService.updateRole(model, qx) > 0 ? "保存成功!" : "保存失败!";
				sysLogService.add(czr, "", "修改角色_" + id, "", "", "", "");
			}
			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/RoleManage/DelRole", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String DelRole(HttpServletRequest request) {
		try {

			String czr = request.getParameter("czr");
			String resultString = "";
			String id = request.getParameter("id");
			resultString = sysService.deleteRole(id) > 0 ? "保存成功!" : "保存失败!";
			sysLogService.add(czr, "", "删除角色_" + id, "", "", "", "");

			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}
}
